//
//  favoritesViewController.m
//  giuseppe
//
//  Created by Apple on 15/8/14.
//  Copyright (c) 2015年 GiantUmbrella. All rights reserved.
//

#import "favoritesViewController.h"
#import "favoritesModel.h"
#import "favoritesCollectionViewCell.h"
#import "schoolUniformHeaderView.h"

@interface favoritesViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>{
    UITableView *_favoriteTableView;
    
    NSMutableArray *_favoritesModelArray;
    UICollectionView *homeCollectionView;

}

@end

@implementation favoritesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //自定义的导航栏
    UIView *navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    navView.backgroundColor = [UIColor whiteColor];
    navView.alpha = 0.90;
    
    //设置内容置顶
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(10, 33, 27, 16);
    [backButton setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth/2-50, 20, 100, 40)];
    titleLabel.text = @"收藏夹";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    
    [self _initHomeCollectionView];
    
    [navView addSubview:backButton];
    [navView addSubview:titleLabel];
    [self.view addSubview:navView];
    
    
}

- (void)loadData{
        
    //    NSString *userId = [[userManager shardInfo].userArray[0] objectForKey:@"userIdentityNumber"];
        
    NSString *userId = @"1";
    NSString *Id = @" ";
    
    NSDictionary *paras = @{@"userId":userId,
                            @"Id":Id,
                            @"type":@"0",

                            };
    
    NSLog(@"paras%@",paras);
        
    //调用请求
    [[myDataAccess shardWebUtil] addtoFavoriteListsWithURLString:kNetPath_Code_favoriteList andParas:paras andTransferGottenData:^(id obj, NSError *error)  {
            
        NSLog(@"%@",obj);
            
        if (obj) {
            
            NSArray *dataArray = [obj objectForKey:@"dataList"];
                
            _favoritesModelArray = [[NSMutableArray alloc]init];
                
            for (NSDictionary *dic in dataArray) {
                    
                favoritesModel *model = [favoritesModel initWithDicAttributes:dic];
                [_favoritesModelArray addObject:model];
                    
            }
            
            [homeCollectionView.legendHeader endRefreshing];
            
            [homeCollectionView reloadData];
        }else{

        }
  
    }];
    
}

#pragma mark viewLoad
- (void)_initHomeCollectionView{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(kScreenWidth/2, 64);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 10;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    [flowLayout setHeaderReferenceSize:CGSizeMake(kScreenWidth,64)];
    //    flowLayout.headerReferenceSize = CGSizeMake(self.view.bounds.size.width, 500);
    
    
    homeCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    homeCollectionView.backgroundColor = [UIColor whiteColor];
    
    homeCollectionView.dataSource = self;
    homeCollectionView.delegate = self;
    
    //注册单元格
    [homeCollectionView registerClass:[favoritesCollectionViewCell class] forCellWithReuseIdentifier:@"collectionViewCell"];
    //注册头视图
    [homeCollectionView registerClass:[schoolUniformHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
    
    [self.view addSubview:homeCollectionView];
    
    
    __weak __typeof(self) weakSelf = self;
    
    [homeCollectionView  addLegendHeaderWithRefreshingBlock:^{
        
        [weakSelf loadData];
        
    }];
    
    [homeCollectionView.legendHeader beginRefreshing];
    
    
}


#pragma mark - UICollectionViewDataSource Methods
#pragma mark 设置某个分组有多少行
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return _favoritesModelArray.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}

#pragma mark 设置某个Item显示什么内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *ID = @"collectionViewCell";
    
    favoritesCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    
    cell.imageView.frame = CGRectMake(0, 0, kScreenWidth/2, 200);
    cell.nameLabel.frame = CGRectMake(0, 200, kScreenWidth/2, 20);
    cell.soldNumberLabel.frame = CGRectMake(0, 220, kScreenWidth/2, 20);
    cell.addShoppingCartButton.frame = CGRectMake(0, 250, kScreenWidth/2, 50);
    cell.model = _favoritesModelArray[indexPath.row];
    
    return cell;
}

#pragma mark 处理点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"我被点击了");
}

#pragma mark - UICollectionViewDelegateFlowLayout Method
#pragma mark 设置item的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(kScreenWidth/2, 300);
}


//设置头尾部内容
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableView =nil;
    if (kind ==UICollectionElementKindSectionHeader) {
        //定制头部视图的内容
        
        schoolUniformHeaderView *headerView = (schoolUniformHeaderView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];
        
        headerView.frame = CGRectMake(0, 0, kScreenWidth, 64);
        
    
  
        reusableView = headerView;
        
        
        
    }
    return reusableView;
}





- (void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
